#include "my_file_system.h"

using namespace std;
using namespace boost::filesystem;



//	set below object as global variable to reduce copy time 
map<string, vector<ASession> > userLog;
vector<string> files;

void getUserLog(const string &filename) {
	path p(filename);	

	// check if filename exist 
	try  {
		if  (exists(p))  {
			if(is_regular_file(p))  {
				cout << "User log file is found!" << endl;			
			} else if  (is_directory(p))  {
				cout << "Error!" << p << " is directory" << endl;
			} else {
				cout << "Error!" << p << " is not a regular file nor directory" << endl;
			}
		}  else  {
			cout << p << " does not exist!" << endl;
		}
	}  catch(const filesystem_error &ex)  {
		cout << ex.what() << endl;
	}

	//	read data into map
	//	input format IP_address N jaccount1 time1 time2 
	//	map<string, vector<ASession> > userLog
	freopen(filename.c_str(), "r", stdin);
	string ip_addr, jac, st_time, ed_time;
	unsigned int jac_num;
	while(cin >> ip_addr) {
		cin >> jac_num;
		vector<ASession> tmp;
		for(size_t i = 0; i < jac_num; i ++)  {
			cin >> jac >> st_time >> ed_time;
			tmp.push_back(ASession(st_time, ed_time, jac));
		}
		userLog[ip_addr] = tmp;
	}
}
/*
 *	test getUserLog
getUserLog("C:\\Users\\vincent\\Dropbox\\paper\\RE_data\\Jan4IPAccount.txt");
	cout << userLog.size() << endl;;
	map<string, vector<ASession>>::iterator it_map = userLog.begin();
	for(int i = 0; i < it_map->second.size(); i ++) {
		cout << it_map->second[i].jaccount_ << " ";
		cout << it_map->second[i].start_time_ << " ";
		cout << it_map->second[i].end_time_ <<endl;
	}
*/

void getFileName(const string &dirname) {
	path p(dirname.c_str());	

	// check if filename exist 
	try  {
		if  (exists(p))  {
			if(is_regular_file(p))  {
				cout << "User log file is found!" << endl;			
			} else if  (is_directory(p))  {
				cout << "Error!" << p << " is directory" << endl;
			} else {
				cout << "Error!" << p << " is not a regular file nor directory" << endl;
			}
		}  else  {
			cout << p << " does not exist!" << endl;
		}
	}  catch(const filesystem_error &ex)  {
		cout << ex.what() << endl;
	}

	// store filenames into vector
	vector<path> tmp;
	copy(directory_iterator(p), directory_iterator(), back_inserter(tmp));

	for(vector<path>::iterator it_vec = tmp.begin(); it_vec != tmp.end(); it_vec ++) {
		if(is_regular_file(*it_vec))  {
			files.push_back(it_vec->string());
			//cout << it_vec->string() << " ";
		}
		//cout << endl;
	}
	
}

//  require high performance
//  data will be write would extends 3TB
//	do not know how to increase performance, however...
void writeToFile(const string&dirname, const map<string, vector<string>> &traffic, int type) {
	string type_name = (type == 0) ? "downstream" : "upstream";
	map<string, vector<string>>::const_iterator it_map;

	for(it_map = traffic.begin(); it_map != traffic.end(); it_map ++)  {
		string jac_name = it_map->first;
		string absolute_path(dirname+"//"+jac_name+"//"+type_name);
		
		cout << absolute_path << endl;

		vector<string>::const_iterator it_vec;
		FILE *written_file = fopen(absolute_path.c_str(), "a");
		for(it_vec = it_map->second.begin(); it_vec != it_map->second.end(); it_vec ++)  {
			string cp_data(*it_vec);
			cp_data.push_back('\n');
			fwrite(cp_data.c_str(), sizeof(char), sizeof(cp_data), written_file);
		}
		fclose(written_file);
	}
}

